package com.camel.base.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.camel.base.feign.AutoCallServiceService;
import com.camel.base.feign.CtiManagerService;
import com.camel.base.model.ZsQueue;
import com.camel.base.model.ZsSeat;
import com.camel.base.service.ZsQueueService;
import com.camel.base.service.ZsSeatService;
import com.camel.core.config.CtiConfig;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.Result;
import com.camel.core.exceptions.CustomException;
import com.camel.core.service.BaseCommonService;
import com.camel.core.utils.ResultUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author camel
 */
@RestController
@RequestMapping("zsSeat")
public class ZsSeatController extends BaseCommonController<ZsSeat> {

    @Autowired
    private ZsSeatService<ZsSeat> service;

    @Autowired
    private ZsQueueService<ZsQueue> queueService;

    @Autowired
    private CtiConfig ctiConfig;

    @Autowired
    private CtiManagerService ctiManagerService;

    @Override
    public boolean deleteAble(Serializable id) {
        ZsSeat byId = service.getById(id);
        if (ObjectUtils.isEmpty(byId)) {
            throw new CustomException("删除失败: " + getMouduleName() + "不存在");
        }
        return true;
    }

    @PostMapping("sync")
    public Result sync(@RequestBody JSONObject params) {
        if (!params.containsKey("queueNum")) {
            return ResultUtil.error(500, "无效的队列");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("queueNum", params.getString("queueNum"));
        Result queueSeats = ctiManagerService.getQueueSeats(jsonObject);

        if (queueSeats.isSuccess()) {
            return ResultUtil.success("同步成功");
        }
        return ResultUtil.error(500, "同步失败");
    }

    @PostMapping("syncAll")
    public Result syncAll(@RequestBody JSONObject params) {
        JSONObject jsonObject = new JSONObject();
        Result queueSeats = ctiManagerService.getQueueSeats(jsonObject);
        if (queueSeats.isSuccess()) {
            ArrayList<LinkedHashMap> queueMaps = (ArrayList<LinkedHashMap>) queueSeats.getData();
            // 更新队列
            for (LinkedHashMap map : queueMaps) {
                ZsQueue queue = new ObjectMapper().convertValue(map, ZsQueue.class);
                List<ZsSeat> agents = queue.getAgents();
                QueryWrapper<ZsQueue> wrapper = new QueryWrapper<ZsQueue>().eq("queue_num", queue.getQueueNum());
                if (queueService.count(wrapper) == 0) {
                    queueService.save(queue);
                } else {
                    queue = queueService.getOne(wrapper, false);
                }
                if (!ObjectUtils.isEmpty(queue)) {
                    saveOrUpdateAgent(agents, queue.getId());
                }
            }
            return ResultUtil.success("同步成功", queueSeats);
        }
        return ResultUtil.error(500, "同步失败");
    }

    void saveOrUpdateAgent(List<ZsSeat> agents, Integer queueId) {
        for (ZsSeat agent : agents) {
            if(StringUtils.isEmpty(agent.getPassword())) {
                agent.setPassword(ctiConfig.getPassword());
            }
            agent.setQueueId(queueId);
            service.saveOrUpdate(agent, new UpdateWrapper<ZsSeat>().eq("agentnum", agent.getAgentnum()));
        }
    }

    @Override
    public boolean exist(ZsSeat entity) {
        return service.count(new QueryWrapper<ZsSeat>().eq("agentnum", entity.getAgentnum())) > 0;
    }

    @Override
    public BaseCommonService<ZsSeat> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "坐席";
    }
}
